<?php
	
	$user = $controller->getCurrentUser();
	//on verifie les permissions
	if ($user->isAllowedTo(GRADE_MOD_BOOK))
	{    
		$isSubmitted = $request->getParam('submitted');
		if ($isSubmitted)
		{
			$nameElement = $request->getParam('nameElement');
			$typeElement = $request->getParam('typeElement');
			
			if (!in_array($typeElement,array('drawer','scriptWritter','publisher','serie')))
				throw new exceptionErrorManual(0,'mauvais type d\'element passe',__file__, __line__);
			else if (!$nameElement || !is_string($nameElement))
			{
				$controller->addError('Veuillez remplir tous les champs !');
				$controller->render('errorAjax',true);
			}
			else if (call_user_func(array($typeElement, 'exist'), $nameElement))
			{
				$controller->addError('Cet &eacute;l&eacute;ment existe d&eacute;j&agrave; sous ce nom !');
				$controller->render('errorAjax',true);            
			}
			else if ($typeElement == 'serie')
			{
				$genre = $request->getParam('genreSerie');
				$type = $request->getParam('typeSerie');
				$stateBDBDEC = $request->getParam('stateBDBDEC');
				$statePublisher = $request->getParam('statePublisher');
				$keywords = $request->getParam('keywordsSerie');
				
				
				$serie = new serie();
				$serie->setGenre($genre);
				$serie->setTypeSerie($type);
				if ($keywords)
					$serie->setKeywords($keywords);
				$serie->setName($nameElement);
				$serie->setStateBDBDEC($stateBDBDEC);
				$serie->setStatePublisher($statePublisher);

				if ($serie->readyToAdd())
				{
					$idElement = $serie->add();
					$logPermAction = 'S&eacute;rie  ajout&eacute;e : '.addslashes($serie->getName());
			
					logPermactions($user->getId(), $logPermAction);
					if (!is_numeric($idElement) || !$idElement)
						throw new exceptionErrorManual(0,'mauvais id d\'element retourne par ajax',__file__, __line__);    
					$controller->setVar('idElement',$idElement);
					$controller->setVar('typeElement',$typeElement);
					$controller->setVar('nameElement',$nameElement);
					$controller->render('ajaxCongratulations', true);
				}
				else
				{
					$controller->addError('Veuillez remplir tous les champs !');
					$controller->render('errorAjax',true);  
				}
			}
			else 
			{
				$objetElement = call_user_func(array($typeElement, 'singleton'));
				$idElement = $objetElement->add($nameElement);
				$logPermAction = $typeElement.' ajout&eacute; : '.addslashes($nameElement);
				logPermactions($user->getId(), $logPermAction);
				if (!is_numeric($idElement) || !$idElement)
					throw new exceptionErrorManual(0,'mauvais id d\'element retourne par ajax',__file__, __line__);    
				
				$controller->setVar('idElement',$idElement);
				$controller->setVar('typeElement',$typeElement);
				$controller->setVar('nameElement',$nameElement);
				$controller->render('ajaxCongratulations', true);
			}
		}
		else
		{
			$typeElement = $controller->getRequest()->getParam('typeElement');
			if (!$typeElement)
				throw new exceptionErrorManual(0,'typeElement attendu en get',__file__,__line__);
			$controller->setVar('typeElement',$typeElement);
			$controller->render('ajaxAddElementForm', true);
		}
	}
	else
		$controller->render('errorRights', true);
	
?>
